package com.scalar.chiptrack.adaptor.ikanos.jsi;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.users.VendorAdaptor;
import com.scalar.chiptrack.users.dao.AdaptorSetUpDAO;
import com.scalar.ScalarException;

import javax.mail.Message;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 2 Sep, 2009
 * Time: 3:30:32 PM
 * To change this template use File | Settings | File Templates.
 */
public class JSIPOP3Adaptor extends JobHandler implements AdaptorConstants
{

    private static final String HOST                = "mail.scalarsoft.com";

    public static final String ADAPTOR_NAME         = "JSI_SG_FG";
    private static final String SUBJECT_DAILY_INC_WIP = "JSG Ikanos Inc Daily Incoming Activities Report";
    private static final String SUBJECT_DAILY_SG_WIP  = "JSG Ikanos Sg Daily Incoming Activities Report";
    private static final String SUBJECT_DAILY_INC_SHIP    = "JSG Ikanos Inc Daily Shipment  Report";
    private static final String SUBJECT_DAILY_SG_SHIP    = "JSG Ikanos Sg Daily Shipment  Report";

    public static final String JSI_SG_WIP_FILE      = "IKANOS INV REPORT";
    
    public static final String JSI_SG_SHIP_FILE     = "SHIPPING LOG";
													
    private Logger m_adaptorLogger      = null;
    private String m_dataOutputDir      = null;
    private String m_username           = null;
    private String m_password           = null;
    private int    m_adaptor_id     = -1;
    /**
     *
     * @throws Exception
     */
    public JSIPOP3Adaptor() throws Exception
    {
        try
        {
            VendorAdaptor vendorAdaptor = AdaptorSetUpDAO.getVendorAdaptorDetailsByVendorNameAndLocation( VENDOR_NAME_JSI_SG, INVENTORY_TYPE_FINISHED_GOODS);

            m_username = vendorAdaptor.getUserId();
            m_password = vendorAdaptor.getPassWord();
            m_dataOutputDir = vendorAdaptor.getOutPutDirectory();

            if ( m_dataOutputDir != null )
            {
                if (m_dataOutputDir.endsWith("/") || m_dataOutputDir.endsWith("\\"))
                {
                    m_dataOutputDir = m_dataOutputDir + m_username;
                }
                else
                {
                    m_dataOutputDir = m_dataOutputDir + "/" + m_username;
                }
               AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
               m_adaptorLogger = AdaptorUtils.getAdaptorLogger( m_dataOutputDir, VENDOR_NAME_JSI_SG );
               AdaptorUtils.setLogger( m_adaptorLogger );
            }
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading JSI FG POP3 Adaptor Username, Password details from database, Exception: " + se);
            throw se;
        }
    }

    //Main Method
    public static void main(String[] args) throws Exception
    {
        new JSIPOP3Adaptor().execute();
    }
    /**
     *
     * @throws Exception
     */
    public void execute() throws Exception
    {
        try
        {
            m_adaptor_id = AdaptorRunInfo.saveAdaptorLastUpdatedStatus( CONSTANT_ADAPTOR, ADAPTOR_NAME, CONSTANT_SUCCESS, -1);

            //Print the header details
            AdaptorUtils.printHeader(VENDOR_NAME_JSI_SG , CONSTANT_ADAPTOR);

            AdaptorUtils.removeOldFiles( m_dataOutputDir, m_adaptorLogger );

            // Step 3
            //Retrive All the Messages
            Message[] messages = AdaptorUtils.getAllPOP3InboxMessages( HOST, m_username, m_password);
            // Step 4
            // Retrieve & Saves the latest message
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_DAILY_SG_WIP,    m_dataOutputDir, FILE_EXTN_XLS, null );
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_DAILY_INC_WIP,   m_dataOutputDir, FILE_EXTN_XLS, null );
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_DAILY_SG_SHIP,   m_dataOutputDir, FILE_EXTN_XLS, null );
            AdaptorUtils.readLatestMessage( m_adaptor_id, messages, SUBJECT_DAILY_INC_SHIP,  m_dataOutputDir, FILE_EXTN_XLS, null );
        }
        catch (Exception e)
        {
            AdaptorUtils.removeOldFiles(m_dataOutputDir, m_adaptorLogger);
            AdaptorRunInfo.updatedAdaptorStatus( CONSTANT_FAILURE, m_adaptor_id);
            System.out.println("Exception while trying to connect and read data from " + HOST +" site: " + e);
            log("Exception while trying to connect and read data from " + HOST +" site: " + e);
            e.printStackTrace();
            throw e;
        }
    }

    private void log(String message)
    {
        if (message == null) return;

        if (m_adaptorLogger != null)
        {
            synchronized (m_adaptorLogger)
            {
                m_adaptorLogger.logDebug(message + LINE_SEPARATOR);
            }
        }
        else
        {
            System.out.println(message);
        }
    }
}
